#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2016-11-11 15:19:57
# @Author  : PiaoYun (piaoyunsoft@163.com)
# @Link    : http://www.dllhook.com
# @Version : $Id$

from Crypto.Cipher import AES
from Crypto import Random
import base64

BS = AES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)

def AESEncryptECB(key, message, isBlock):
    if isBlock:
        key = key.decode("hex")  # key长度必须为16/24/32
        message = message.decode("hex")

    cipher = AES.new(key, AES.MODE_ECB)
    ss = cipher.encrypt(message)
    return ss.encode("hex").upper()
    # return base64.b64encode(ss)

def AESDecryptECB(key, message, isBlock):
    if isBlock:
        key = key.decode("hex")  # key长度必须为16/24/32
        #message = message.decode("hex")

    cipher = AES.new(key, AES.MODE_ECB)
    ss = cipher.decrypt(message.decode("hex"))

    if isBlock:
    	return ss.encode("hex")

    return ss

if __name__ == '__main__':

    print("====string test====")
    print("128Bit:%s" %AESEncryptECB("1234567812345678", "12345678123456781234567812345678", False))	# 128Bit
    print("192Bit:%s" %AESEncryptECB("123456781234567812345678", "12345678123456781234567812345678", False))	# 192Bit
    print("256Bit:%s" %AESEncryptECB("12345678123456781234567812345678", "12345678123456781234567812345678", False))	# 256Bit

    print("====blcok test====")
    print("128Bit:%s" %AESEncryptECB("12345678123456781234567812345678", "12345678123456781234567812345678", True))
    print("192Bit:%s" %AESEncryptECB("123456781234567812345678123456781234567812345678", "12345678123456781234567812345678", True))
    print("256Bit:%s" %AESEncryptECB("1234567812345678123456781234567812345678123456781234567812345678", "12345678123456781234567812345678", True))

    print("====string encrypt test====")
    print("128Bit:%s" %AESDecryptECB("1234567812345678", "6DAC1C56E747FAE03ACF8C6891E428E06DAC1C56E747FAE03ACF8C6891E428E0", False))	# 128Bit
    print("====decrypt encrypt test====")
    print("128Bit:%s" %AESDecryptECB("12345678123456781234567812345678", "D7EEEE18C420FAF0DC7DB5CA73A2B817", True))
